package network.handler;

import java.io.IOException;

import message.Message;
import message.MessageAcceptor;
import message.MessageParser;
import message.types.unique.EmptyMessage;
import network.e2e.FrameReader;

/**
 * Relays message from the net to the stalker
 */

public class StalkerNetListener implements Runnable {
  
  private MessageAcceptor acceptor_;
  private FrameReader reader_;
  
  /**
   * Constructs a listener that connects reader to acceptor
   * @requires both param no null
   * @param acceptor the target
   * @param reader  the net
   */
  public StalkerNetListener(MessageAcceptor acceptor, FrameReader reader) {
    acceptor_ = acceptor;
    reader_ = reader;
  }

  public void run() {
    while (true) {
      Message message;
      try {
        message = MessageParser.retrieve(reader_);
      } catch (IOException e) {
        System.out.println("Failure happened, terminating this");
        return;
      }
      if (message instanceof EmptyMessage) {
        //done
        return;
      } else {
        acceptor_.acceptMessage(message);
      }
    }
  }
  
  
  
  
  
}
